package com.songtech.server.frame.service;

import com.songtech.server.Jedis.JedisBaseDao;
import com.songtech.server.constant.KeysConstant;
import com.songtech.server.constant.TcpServer;
import com.songtech.server.entry.BaseProtocal;
import com.songtech.server.entry.PositionProtocal;
import com.songtech.server.frame.chain.ChainManager;
import com.songtech.server.frame.chain.handler.ElefenceHandler;
import com.songtech.server.frame.chain.handler.EquStatusHandler;
import com.songtech.server.frame.chain.handler.StoragePolicyHandler;
import com.songtech.server.frame.dao.mapper.PositionServerMapper;
import com.songtech.server.frame.route.annotation.Allot;
import com.songtech.server.frame.protocal.AssembleFactory;
import com.songtech.server.utils.GetKeysUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import com.songtech.server.constant.Calculate;
import com.songtech.server.frame.push.Push;
import org.springframework.beans.factory.annotation.Autowired;
import com.songtech.server.utils.CalculationUtils;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.time.LocalTime;

/**
 * Create By YINN on 2018/4/7 2:28
 * Description : 测试service，各service通过@Allot(cmd = "命令值")分辨各协议从而进入不同service,
 * 所有业务service都需要实现BaseService接口!!!
 */

@Allot(cmd = "V1", beanClass = PositionProtocal.class)
@Service
public class PositionServerServiceImpl implements BaseService {

    private static final String INVALID = "V";

    @Autowired
    protected Push push;

    @Resource
    @Qualifier("jedisBaseDaoImpl")
    protected JedisBaseDao<Serializable> jedisBaseDao;

    @Autowired
    protected PositionServerMapper positionServerMapper;

    @Override
//    @Transactional String manufacturer, String imei, String cmd, LocalTime cmdTime, String[] cmdParam
    public void invoke(BaseProtocal baseProtocal) {

        PositionProtocal entry = (PositionProtocal) baseProtocal;
        entry.setAll();

        //判断数据有效性
        String valid = entry.getDataValidFigure();
        if (INVALID.equals(valid)) {
            return;
        }

        //设置定位类型
        entry.setLocationType(TcpServer.GPS);


        //获取电量信息
        String dumpEnergyKey = GetKeysUtils.combineAnyKeys(KeysConstant.DUMP_ENERGY, entry.getImei());
        entry.setDumpEnergy((String) jedisBaseDao.get(dumpEnergyKey));


        //添加电子围栏，其他类型告警,距离、时间精度变化处理链 并执行处理器

        ChainManager chainManager = new ChainManager();

        chainManager.addLast(new ElefenceHandler(entry,jedisBaseDao))
                    .addLast(new EquStatusHandler(entry,jedisBaseDao))
                    .addLast(new StoragePolicyHandler(entry,jedisBaseDao,positionServerMapper))
                    .trigger();


        //刷新redis最新定位
        jedisBaseDao.put(entry.getRedisKey(), entry);

        //存储地图展示键
        jedisBaseDao.put(GetKeysUtils.combineAnyKeys(KeysConstant.TERMINAL_POSITION_SHOW_KEY,entry.getImei()), entry);
    }
}
